﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Report
{
    public partial class frmSearch : Form
    {
        SQL sql = new SQL();
        private string date = "";
        private string id = "";
        private string dvtt_name = "";
        private string dvtt_id = "";
        private string dvtt_bank = "";
        private string dvtt_addr = "";
        private string dvtt_amount = "";
        private string dvh_name = "";
        private string dvh_id = "";
        private string dvh_bank = "";
        private string dvh_addr = "";
        private string dvh_amount = "";
        private string content = "";
        private string money = "";
        private int include=0;
        private int exclude = 0;
        public frmSearch()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            SqlParameter[] pr = new SqlParameter[11];
            pr[0] = new SqlParameter("@Date", dateTimePicker1.Value.ToString("MM/dd/yyyy"));
            pr[1] = new SqlParameter("@ID", tbID.Text.Trim());
            pr[2] = new SqlParameter("@DVTT_NAME", tb_search_dvtt_name.Text.Trim());
            pr[3] = new SqlParameter("@DVTT_ID", tb_search_dvtt_id.Text.Trim());
            pr[4] = new SqlParameter("@DVTT_BANK", tb_search_dvtt_bank.Text.Trim());
            pr[5] = new SqlParameter("@DVTT_ADDR", tb_search_dvtt_addr.Text.Trim());
            pr[6] = new SqlParameter("@DVH_NAME", tb_search_dvh_name.Text.Trim());
            pr[7] = new SqlParameter("@DVH_ID", tb_search_dvh_id.Text.Trim());
            pr[8] = new SqlParameter("@DVH_BANK", tb_search_dvh_bank.Text.Trim());
            pr[9] = new SqlParameter("@DVH_ADDR", tb_search_dvh_addr.Text.Trim());
            pr[10] = new SqlParameter("@Func", 0);
            //dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            DataTable dt = sql.getDataTable("sql_search_UNC", pr);
            dt.Columns[0].ColumnName = "Ngày lập";
            dt.Columns[1].ColumnName = "Số";
            dt.Columns[2].ColumnName = "Tên đơn vị trả tiền";
            dt.Columns[3].ColumnName = "DVTT_Số_tài_khoản";
            dt.Columns[4].ColumnName = "DVTT_Ngân_hàng";
            dt.Columns[5].ColumnName = "ĐVTT_Địa_chỉ";
            dt.Columns[6].ColumnName = "ĐVTT_Tiền_nợ";
            dt.Columns[7].ColumnName = "Tên đơn vị hưởng";
            dt.Columns[8].ColumnName = "ĐVH_Số_tài_khoản";
            dt.Columns[9].ColumnName = "ĐVH_Ngân_hàng ";
            dt.Columns[10].ColumnName = "ĐVH_Địa_chỉ";
            dt.Columns[11].ColumnName = "Tiền có";
            dt.Columns[12].ColumnName = "Nội dung";
            dt.Columns[13].ColumnName = "Số tiền";

            dataGridView1.DataSource = dt;

        }

        private void getValue(int index)
        {
            try
            {
                date = dataGridView1.Rows[index].Cells[0].Value.ToString().Trim();
                id = dataGridView1.Rows[index].Cells[1].Value.ToString().Trim();
                dvtt_name = dataGridView1.Rows[index].Cells[2].Value.ToString().Trim();
                dvtt_id = dataGridView1.Rows[index].Cells[3].Value.ToString().Trim();
                dvtt_bank = dataGridView1.Rows[index].Cells[4].Value.ToString().Trim();
                dvtt_addr = dataGridView1.Rows[index].Cells[5].Value.ToString().Trim();
                dvtt_amount = dataGridView1.Rows[index].Cells[6].Value.ToString().Trim();
                dvh_name = dataGridView1.Rows[index].Cells[7].Value.ToString().Trim();
                dvh_id = dataGridView1.Rows[index].Cells[8].Value.ToString().Trim();
                dvh_bank = dataGridView1.Rows[index].Cells[9].Value.ToString().Trim();
                dvh_addr = dataGridView1.Rows[index].Cells[10].Value.ToString().Trim();
                dvh_amount = dataGridView1.Rows[index].Cells[11].Value.ToString().Trim();
                content = dataGridView1.Rows[index].Cells[12].Value.ToString().Trim();
                money = dataGridView1.Rows[index].Cells[13].Value.ToString().Trim();
                include = Convert.ToInt32(dataGridView1.Rows[index].Cells[14].Value.ToString().Trim());
                exclude = Convert.ToInt32(dataGridView1.Rows[index].Cells[15].Value.ToString().Trim());
            }
            catch (Exception ex)
            {
                MessageBox.Show("Không có dữ liệu", ex.Message);
            }

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.RowIndex;
            getValue(index);
            Form1 frm1 = new Form1(date, id, dvtt_name, dvtt_id, dvtt_bank, dvtt_addr, dvtt_amount, dvh_name, dvh_id, dvh_bank, dvh_addr, dvh_amount, content, money,include,exclude);
            frm1.ShowDialog();
        }
    }
}
